﻿namespace ScrumFactory.Services.Logic.Tests
{
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;
    using ScrumFactory;
    using ScrumFactory.Data;
    using ScrumFactory.Services.Logic;

    [TestClass()]
    public class ProjectsServiceTest
    {
        [TestMethod()]
        public void GetAllProjectsTest()
        {
            var projectsRepository = new Mock<IProjectRepository>();

            var project1 = new Project();
            var projects = new List<Project> { project1 };

            projectsRepository.Setup(r => r.GetAllProjects()).Returns(projects);

            var target = new ProjectsService(projectsRepository.Object);

            var result = target.GetAllProjects();

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.AreSame(project1, result.First());
        }

        [TestMethod()]
        public void GetProjectByIdTest()
        {
            var projectsRepository = new Mock<IProjectRepository>();

            var project1 = new Project();

            projectsRepository.Setup(r => r.GetProject("project1")).Returns(project1);

            var target = new ProjectsService(projectsRepository.Object);

            var result = target.GetProject("project1");

            Assert.IsNotNull(result);
            Assert.AreSame(project1, result);
        }

        [TestMethod()]
        public void SaveNewProject()
        {
            var projectsRepository = new Mock<IProjectRepository>();

            projectsRepository.Setup(r => r.GetProject("project1")).Returns(default(Project));

            var target = new ProjectsService(projectsRepository.Object);

            var project1 = new Project { ProjectUId = "project1", ProjectName = "Project 1" };

            target.CreateProject(project1);

            projectsRepository.Verify(r => r.GetProject("project1"), "Did not queried for project existence");
            projectsRepository.Verify(r => r.InsertProject(project1), "Did not inserted new project");
        }

        [TestMethod()]
        public void UpdateProject()
        {
            var projectsRepository = new Mock<IProjectRepository>();

            projectsRepository.Setup(r => r.GetProject("project1")).Returns(new Project { ProjectUId = "project1", ProjectName = "Old name" });

            var target = new ProjectsService(projectsRepository.Object);

            var project1 = new Project { ProjectUId = "project1", ProjectName = "Project 1" };

            target.UpdateProject(project1);

            projectsRepository.Verify(r => r.GetProject("project1"), "Did not queried for project existence");
            projectsRepository.Verify(r => r.UpdateProject(project1), "Did not update project");
        }
    }
}
